package com.hutiao.snowhb.actions.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.entities.Page;
import com.hutiao.snowhb.entities.PhotographerBean;
import com.hutiao.snowhb.service.PhotographerService;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.utility.StringUtil;

@Namespace("/admin")
public class AdminPhotosAction extends ActionSupport {

	private static final long serialVersionUID = -7700244078884038763L;
	
	private static Logger logger = Logger.getLogger(AdminPhotosAction.class);

	@Autowired
	private PhotographerService photographerService;
	
	private Page photoPage = new Page(1);
	
	private int photoId;
	
	private String result;
	
//	private List<List<PhotographerBean>> photosList = new ArrayList<List<PhotographerBean>>();
//	
//	private int pageNo = 1;
//	
//	private int pageTotal;
//	
//	private int photoTotal;
	
	@JSON(serialize=false)
	public PhotographerService getPhotographerService() {
		return photographerService;
	}

	public void setPhotographerService(PhotographerService photographerService) {
		this.photographerService = photographerService;
	}

//	@JSON(serialize=false)
//	public List<List<PhotographerBean>> getPhotosList() {
//		return photosList;
//	}
//
//	public void setPhotosList(List<List<PhotographerBean>> photosList) {
//		this.photosList = photosList;
//	}

	@JSON(serialize=false)
	public int getPhotoId() {
		return photoId;
	}

	public void setPhotoId(int photoId) {
		this.photoId = photoId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

//	@JSON(serialize=false)
//	public int getPageNo() {
//		return pageNo;
//	}
//
//	public void setPageNo(int pageNo) {
//		this.pageNo = pageNo;
//	}
//	
//	@JSON(serialize=false)
//	public int getPageTotal() {
//		return pageTotal;
//	}
//
//	public void setPageTotal(int pageTotal) {
//		this.pageTotal = pageTotal;
//	}
//
//	@JSON(serialize=false)
//	public int getPhotoTotal() {
//		return photoTotal;
//	}
//
//	public void setPhotoTotal(int photoTotal) {
//		this.photoTotal = photoTotal;
//	}

	@JSON(serialize=false)
	public Page getPhotoPage() {
		return photoPage;
	}

	public void setPhotoPage(Page photoPage) {
		this.photoPage = photoPage;
	}

	@Action(value="photographer_examined")
	public String examined() {
		
		int status = 1;
		int n = 20;
		int rowCount = this.photographerService.getPhotosCount(status);
		
		int totalPage = (rowCount%n)==0 ? (rowCount/n) : (rowCount/n + 1);
		if (this.photoPage.getCurrentPage() > totalPage) {
			this.photoPage.setCurrentPage(totalPage);
		}
		
		if (this.photoPage.getCurrentPage() == 0) {
			this.photoPage.setCurrentPage(1);
		}

		List<List<PhotographerBean>> photosList = new ArrayList<List<PhotographerBean>>();
		List<PhotographerBean> photos = this.photographerService.getPhotosByPage(this.photoPage.getCurrentPage(), n, status);
		
		if (photos==null || photos.size()==0) {
			return SUCCESS;
		}
		
		List<PhotographerBean> temp = new ArrayList<PhotographerBean>();
		for (PhotographerBean photo : photos) {
			temp.add(photo);
			if (temp.size() == 4) {
				photosList.add(new ArrayList<PhotographerBean>(temp));
				temp.clear();
			}
		}
		if (temp.size()>0) {
			photosList.add(new ArrayList<PhotographerBean>(temp));
		}
		
		this.photoPage.build(n, rowCount, photosList);
		
		return SUCCESS;
	}
	
	@Action(value="photographer_unexamine")
	public String unexamine() {
		
		int status = 0;
		int n = 20;
		int rowCount = this.photographerService.getPhotosCount(status);
		
		int totalPage = (rowCount%n)==0 ? (rowCount/n) : (rowCount/n + 1);
		
		if (this.photoPage.getCurrentPage() > totalPage) {
			this.photoPage.setCurrentPage(totalPage);
		}
		
		if (this.photoPage.getCurrentPage() == 0) {
			this.photoPage.setCurrentPage(1);
		}

		List<List<PhotographerBean>> photosList = new ArrayList<List<PhotographerBean>>();
		List<PhotographerBean> photos = this.photographerService.getPhotosByPage(this.photoPage.getCurrentPage(), n, status);
		
		if (photos==null || photos.size()==0) {
			return SUCCESS;
		}
		
		List<PhotographerBean> temp = new ArrayList<PhotographerBean>();
		for (PhotographerBean photo : photos) {
			temp.add(photo);
			if (temp.size() == 4) {
				photosList.add(new ArrayList<PhotographerBean>(temp));
				temp.clear();
			}
		}
		if (temp.size()>0) {
			photosList.add(new ArrayList<PhotographerBean>(temp));
		}
		
		this.photoPage.build(n, rowCount, photosList);
		
		return SUCCESS;
	}
	
	@Action(value="photographer_delete",results = { @Result(type = "json") })
	public String delete() {
		
		String sql = "update hd_photographer set status=2,updateTime=now() where id="+this.photoId+"";
		
		this.photographerService.getBaseDao().execute(sql);
		
		this.result = "删除成功！";
		return SUCCESS;
	}
	
	@Action(value="photographer_passexamine",results = { @Result(type = "json") })
	public String passexamine() {
		
		PhotographerBean photo = null;
		
		try {
			photo = this.photographerService.getPhotoById(this.photoId);
		} catch (Exception e) {
			logger.error("Exception", e);
		}
		
		if (photo == null) {
			this.result = "照片不存在";
			return ERROR;
		}
		
		// 下载图片,并生成缩略图
		String[] files = WeixinUtil.downloadMedia(photo.getMediaId());
		if (files == null) {
			files = WeixinUtil.downloadFile(photo.getPhotoUrl(), photo.getMediaId());
		}
		
		if (files == null || files.length != 2) {
			this.result = "发生内部错误";
			return ERROR;
		}
		
		// 更新到数据库
		String full = "http://static.hutiao.me/snow/huodong/photographer/" + photo.getMediaId() + ".jpg";
		String thumb = "http://static.hutiao.me/snow/huodong/photographer/" + photo.getMediaId() + "_400.jpg";
		String sql = "update hd_photographer set status=1,saveFull='"+full+"',saveThumb='"+thumb+"',updateTime=now() where id="+this.photoId+"";
		
		this.photographerService.getBaseDao().execute(sql);
		
		this.result = "更新成功！";
		return SUCCESS;
	}
	
}
